﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

namespace SHomeWorkshop.LunarConcept.Widgets.WidgetsStyleSamples
{
    /// <summary>
    /// 创建时间：2012年6月15日
    /// 创建者：  杨震宇
    /// 
    /// 主要用途：创建一个代表对应部件的样式的CheckBox。以便设置部件默认样式。
    /// 
    /// ★注意★：类名必须以“_WSS”开头。
    /// </summary>
    class _WSSRectangleWidget : WidgetsStyleSamples.WidgetStyleSample
    {
        public _WSSRectangleWidget(WidgetStyle srcStyle, string localName) :
            base(srcStyle, localName)
        {
            this.mainStyle = WidgetStyle.FromWidgetClassName(typeof(Widgets.RectangleWidget).Name);

            this.sampleTextBlock.Text =
                (this.mainStyle != null && string.IsNullOrEmpty(this.mainStyle.Text) == false) ?
                    this.mainStyle.Text :
                    this.WidgetClassLocalName;

            this.Children.Add(this.sampleTextBlock);

            Canvas.SetZIndex(sampleTextBlock, 100);

            Canvas.SetLeft(this.sampleTextBlock, (this.Width - this.sampleTextBlock.Width) / 2);

            this.sampleTextBlock.InvalidateArrange();
            this.sampleTextBlock.UpdateLayout();
            Canvas.SetTop(this.sampleTextBlock, (this.Height - this.sampleTextBlock.Height) / 2);

            this.Children.Add(this.mainShape);

            this.mainShape.Width = 40;
            this.mainShape.Height = this.Height - 24;

            Canvas.SetLeft(this.mainShape, 12);
            Canvas.SetTop(this.mainShape, 12);

            //=========挂接需要的事件。
            this.MainStyle = WidgetStyle.FromWidgetClassName(this.WidgetClassName);

            //this.mainStyle.ArrowsChanged += new EventHandler<ArrowsChangedEventArgs>(mainStyle_ArrowsChanged);//无意义
            this.MainStyle.IsShadowVisibleChanged += new EventHandler<IsShadowVisibleChangedEventArgs>(mainStyle_IsShadowVisibleChanged);
            this.MainStyle.LineDashChanged += new EventHandler<LineDashChangedEventArgs>(mainStyle_LineDashChanged);

            this.MainStyle.WidgetBackColorChanged += new EventHandler<WidgetBackColorChangedEventArgs>(mainStyle_WidgetBackColorChanged);
            this.MainStyle.WidgetLineColorChanged += new EventHandler<WidgetLineColorChangedEventArgs>(mainStyle_WidgetLineColorChanged);
            this.MainStyle.WidgetLineWidthChanged += new EventHandler<WidgetLineWidthChangedEventArgs>(mainStyle_WidgetLineWidthChanged);

            this.MainStyle.WidgetForeColorChanged += new EventHandler<WidgetForeColorChangedEventArgs>(mainStyle_WidgetForeColorChanged);
            this.MainStyle.WidgetOpacityChanged += new EventHandler<WidgetOpacityChangedEventArgs>(mainStyle_WidgetOpacityChanged);
            //this.mainStyle.WidgetPaddingChanged += new EventHandler<WidgetPaddingChangedEventArgs>(mainStyle_WidgetPaddingChanged);//无意义

            this.mainStyle.WidgetRadiusChanged += new EventHandler<WidgetRadiusChangedEventArgs>(mainStyle_WidgetRadiusChanged);
        }

        void mainStyle_WidgetRadiusChanged(object sender, WidgetRadiusChangedEventArgs e)
        {
            if (e.NewValue > 0)
            {
                double k = (e.NewValue % 1);//取小数。
                if (k == 0) k = 1;
                double r = Math.Min(this.mainShape.ActualWidth, this.mainShape.ActualHeight) * k;

                this.mainShape.RadiusX =
                    this.mainShape.RadiusY = r;
            }
            else
            {
                this.mainShape.RadiusX =
                 this.mainShape.RadiusY = 0;
            }
        }

        //void mainStyle_WidgetPaddingChanged(object sender, WidgetPaddingChangedEventArgs e)
        //{
        //
        //}

        void mainStyle_WidgetOpacityChanged(object sender, WidgetOpacityChangedEventArgs e)
        {
            this.mainShape.Opacity = this.sampleTextBlock.Opacity = e.NewValue;
        }

        void mainStyle_WidgetForeColorChanged(object sender, WidgetForeColorChangedEventArgs e)
        {
            this.sampleTextBlock.Foreground = e.NewValue;
        }

        void mainStyle_WidgetLineWidthChanged(object sender, WidgetLineWidthChangedEventArgs e)
        {
            this.mainShape.StrokeThickness = (e.NewValue < 1) ? 1 : e.NewValue;
        }

        void mainStyle_WidgetLineColorChanged(object sender, WidgetLineColorChangedEventArgs e)
        {
            this.mainShape.Stroke = e.NewValue;
        }

        void mainStyle_WidgetBackColorChanged(object sender, WidgetBackColorChangedEventArgs e)
        {
            this.mainShape.Fill = e.NewValue;
        }

        void mainStyle_LineDashChanged(object sender, LineDashChangedEventArgs e)
        {
            switch (e.NewValue)
            {
                case LineDashType.DashType.Dash:
                    {
                        this.mainShape.StrokeDashArray = LineDashType.dashCollection; break;
                    }
                case LineDashType.DashType.DashDotDot:
                    {
                        this.mainShape.StrokeDashArray = LineDashType.dashDotDotCollection; break;
                    }
                case LineDashType.DashType.Dot:
                    {
                        this.mainShape.StrokeDashArray = LineDashType.dotCollection; break;
                    }
                case LineDashType.DashType.DashDot:
                    {
                        this.mainShape.StrokeDashArray = LineDashType.dashDotCollection; break;
                    }
                default:
                    {
                        this.mainShape.StrokeDashArray = LineDashType.solidCollection; break;
                    }
            }
        }

        void mainStyle_IsShadowVisibleChanged(object sender, IsShadowVisibleChangedEventArgs e)
        {
            if (e.NewValue)
            {
                this.Effect = Widget.ShadowEffect;
            }
            else
            {
                this.Effect = null;
            }
        }

        //void mainStyle_ArrowsChanged(object sender, ArrowsChangedEventArgs e)
        //{
        //}

        private Rectangle mainShape = new Rectangle()
        {
            Stroke = Brushes.Black,
            StrokeThickness = 2,
        };
    }
}
